import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test2 {

    @Test
    public void test() throws InterruptedException {

//        Object lockA = new Object();
//        Object lockB = new Object();

        Lock lockA = new ReentrantLock();
        Lock lockB = new ReentrantLock();

        new Thread(() -> {
            synchronized (lockA) {
                System.out.println("拥有lockA锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lockB) {
                    System.out.println("获得B锁");
                }
            }

        }, "A").start();

        //TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            synchronized (lockB) {
                System.out.println("拥有lockB锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lockA) {
                    System.out.println("获得A锁");
                }
            }

        }, "B").start();
    }

}
